//
//  NumberChangeAnimationView.m
//  ZhiHuDaily
//
//  Created by albert on 16/9/1.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "NumberChangeAnimationView.h"

@interface NumberChangeAnimationView ()
/** 要做动画的数字view */
@property (nonatomic, strong) NSMutableArray *numViews;
@end

@implementation NumberChangeAnimationView

/** 字体大小 */
static CGFloat fontSize = 16;
/** 动画时间 */
static CGFloat animationDuration = 0.3;

#pragma mark - 懒加载
- (NSMutableArray *)numViews{
    if (!_numViews) {
        _numViews = [NSMutableArray array];
    }
    return _numViews;
}

#pragma mark - 初始化
+ (instancetype)viewWithOriginalNumber:(int)number{
    NumberChangeAnimationView *view = [[self alloc] init];
    view.originalNumber = number;
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    self.backgroundColor = [JKColor(0, 132, 208) colorWithAlphaComponent:0.7];
    self.clipsToBounds = YES;
    self.alpha = 0;
}

/** 开始动画 */
- (void)beginAnimationWithTransform:(CGAffineTransform)transform complete:(void(^)())complete{
    
    [UIView animateWithDuration:animationDuration animations:^{
        self.transform = transform;
        self.alpha = 1;
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:animationDuration * 2 animations:^{
            for (UIView *vv in self.numViews) {
                vv.transform = CGAffineTransformMakeTranslation(0, -self.height);
            }
            
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:animationDuration delay:animationDuration * 0.5 options:0 animations:^{
                self.transform = CGAffineTransformIdentity;
                self.alpha = 0;
                
            } completion:^(BOOL finished) {
                self.hidden = YES;
                for (UIView *vv in self.numViews) {
                    vv.transform = CGAffineTransformIdentity;
                }
                !complete ? : complete();
            }];
        }];
    }];
}

- (void)setOriginalNumber:(int)originalNumber{
    _originalNumber = originalNumber;
    
    if (self.numViews.count > 0) {
        return;
    }
    
    NSString *originalNumStr = @(self.originalNumber).stringValue;
    NSString *changedNumStr = @(self.originalNumber + 1).stringValue;
    
    NSUInteger originalLength = originalNumStr.length;
    NSUInteger changedLength = changedNumStr.length;
    
    CGFloat width = 10;
    CGFloat height = self.height;
    CGFloat Y = 0;
    CGFloat X = 0;
    
    if (changedLength > originalLength) {
        
        UILabel *lb1 = [[UILabel alloc] init];
        lb1.textColor = [UIColor whiteColor];
        lb1.frame = self.bounds;
        lb1.text = originalNumStr;
        lb1.textAlignment = NSTextAlignmentCenter;
        lb1.font = [UIFont systemFontOfSize:fontSize];
        [self addSubview: lb1];
        [self.numViews addObject:lb1];
        
        UILabel *lb2 = [[UILabel alloc] init];
        lb2.textColor = [UIColor whiteColor];
        lb2.frame = self.bounds;
        lb2.y = height;
        lb2.text = changedNumStr;
        lb2.textAlignment = NSTextAlignmentCenter;
        lb2.font = [UIFont systemFontOfSize:fontSize];
        [self addSubview:lb2];
        [self.numViews addObject:lb2];
        
        return;
    }
    
    for (NSUInteger i = 0; i < changedLength; i++) {
        X = (self.width - width * changedLength) * 0.5 + i * width;
        
        // 顶部
        NSString *originalC = [originalNumStr substringWithRange:NSMakeRange(i, 1)];
        
        // 底部
        NSString *changedC = [changedNumStr substringWithRange:NSMakeRange(i, 1)];
        
        UIView *numV = [[UIView alloc] init];
        numV.frame = CGRectMake(X, Y, width, height * 2);
        [self addSubview:numV];
        [self.numViews addObject:numV];
        
        // 顶部label
        UILabel *topLb = [[UILabel alloc] init];
        topLb.frame = CGRectMake(0, 0, width, height);
        topLb.text = originalC;
        topLb.textColor = [UIColor whiteColor];
        topLb.textAlignment = NSTextAlignmentCenter;
        topLb.font = [UIFont systemFontOfSize:fontSize];
        [numV addSubview:topLb];
        
        if ([originalC isEqualToString:changedC]) {
            topLb.frame = CGRectMake(X, Y, width, height);
            [self addSubview:topLb];
            continue;
        }
        
        // 底部label
        UILabel *bottomLb = [[UILabel alloc] init];
        bottomLb.frame = CGRectMake(0, height, width, height);
        bottomLb.text = changedC;
        bottomLb.textColor = [UIColor whiteColor];
        bottomLb.textAlignment = NSTextAlignmentCenter;
        bottomLb.font = [UIFont systemFontOfSize:fontSize];
        [numV addSubview:bottomLb];
    }
}
@end
